package org.jeecg.modules.supplier.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.supplier.entity.ProcUserSupplierRelation;
import org.jeecg.modules.supplier.entity.Supplier;
import org.jeecg.modules.supplier.service.IProcUserSupplierRelationService;
import org.jeecg.modules.supplier.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
@Api(tags = "供应商用户信息绑定66")
@RestController
@RequestMapping("procurement/userSupRelation")
@Slf4j
public class ProUserSupRelaController {

    @Autowired
    private IProcUserSupplierRelationService procUserSupplierRelationService;
    @Autowired
    private ISupplierService supplierService;

    @AutoLog(value = "供应商用户关联")
    @ApiOperation(value = "供应商用户关联", notes = "供应商用户关联")
    @GetMapping(value = "/addUserInfo")
    public Result<?> addUserInfo(String userId, String supplierId, String Username,String CompanyName) {
        // 获取当前登录用户信息
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String currentUsername = loginUser != null ? loginUser.getUsername() : "";
        Date currentTime = new Date();

        // 首先通过userId查询关联关系
        ProcUserSupplierRelation existingRelation = null;
        if (StringUtils.isNotBlank(userId)) {
            // 使用getRelationBySearchKey方法查询，但需要确保只获取userId匹配的记录
            // 这里使用QueryWrapper进行精确查询
            QueryWrapper<ProcUserSupplierRelation> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userId);
            existingRelation = procUserSupplierRelationService.getOne(queryWrapper);
        }

        boolean result;
        // 判断是否存在记录以及supplierId是否为空
        if (existingRelation != null) {
            // 记录存在，更新信息
            existingRelation.setSupplierId(supplierId);
            existingRelation.setUsername(Username);
            existingRelation.setUpdateBy(currentUsername);
            existingRelation.setCompanyName(CompanyName);
            existingRelation.setUpdateTime(currentTime);
            result = procUserSupplierRelationService.updateById(existingRelation);
        } else {
            // 记录不存在，创建新记录
            ProcUserSupplierRelation userInfo = new ProcUserSupplierRelation();
            userInfo.setUserId(userId);
            userInfo.setSupplierId(supplierId);
            userInfo.setUsername(Username);
            userInfo.setCreateBy(currentUsername);
            userInfo.setCreateTime(currentTime);
            result = procUserSupplierRelationService.save(userInfo);
        }

        return result ? Result.OK(existingRelation != null ? "更新成功！" : "添加成功！") : Result.error(existingRelation != null ? "更新失败！" : "添加失败！");
    }

    //用户设置成为管理员，传入userid，将is_admin设置为1
    @AutoLog(value = "用户设置成为管理员")
    @ApiOperation(value = "用户设置成为管理员", notes = "用户设置成为管理员")
    @GetMapping(value = "/setAdminSupplier")
    public Result<?> setAdminSupplier(String userId) {
        boolean result = procUserSupplierRelationService.setAdminSupplier(userId);
        return result ? Result.OK("设置成功！") : Result.error("设置失败！");
    }
    //查询关联表(proc_user_supplier)中的supplierId，如果proc_supplier表的supplierId不在proc_user_supplier)中则返回。返回列表
    @AutoLog(value = "获取未关联供应商列表")
    @ApiOperation(value="获取未关联供应商列表", notes="获取未关联供应商列表，支持模糊查询")
    @GetMapping(value = "/getUnrelatedSuppliers")
    public Result<?> getUnrelatedSuppliers(String companyName) {
        try {
            List<Supplier>  unrelatedSuppliers = procUserSupplierRelationService.getUnrelatedSuppliers( companyName);

            return Result.OK(unrelatedSuppliers);
        } catch (Exception e) {
            log.error("获取未关联供应商列表失败", e);
            return Result.error("获取未关联供应商列表失败: " + e.getMessage());
        }
    }

}
